<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 10:57
 */
namespace app\admin\controller;
use app\admin\model\Documents;
use app\admin\model\Subject;
use think\Controller;
use think\Db;
use think\Request;

class Document extends Controller
{
    public function show_list(Request $request){
        $cid = $request->param('cid',null);
        $obj = new Subject();
        $obj->setCid($cid);
        $field = $obj->getField();
        $news = [];
        foreach ($field as $value){
            $news[$value['field']] =$value['field_name'];
        }
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('document/del?cid='.$cid)
            ->addTableHeaderAdd('document/show_add?cid='.$cid)
            ->addTableHeaderSousuo('','搜索')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text',$news['name'])
            ->addTableColumn('dir','text',$news['dir'])
            ->addTableColumn('sort','text','排序')
//            ->addTableColumn('cid','text','栏目')
//            ->addTableColumn('model_id','text','模型')
            ->addTableColumn('status','switch','状态',['启用','禁用'],'document/change?cid='.$cid)
            ->addTableColumn('delete','status','是否可删除',['可','不可'])
            ->addTableRightHeader('操作')
            ->addTableRight('detail','document/detail?cid='.$cid,'查看')
            ->addTableRight('edit','document/show_edit?cid='.$cid,'修改')
            ->addTableRight('del','document/del?cid='.$cid,'删除')
            ->addTablePage('document/page?cid='.$cid)
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $tiaojian = $request->param('tiaojian',null);
        $cid = $request->param('cid');
        $obj = new Subject();
        $obj->setCid($cid);
        $model = $obj->getModel();
        $data = Db::name($model['model_table'])
            ->where('delete_time',null);
        if($tiaojian){
            $data = $data->where('name|dir','like',"%$tiaojian%");
        }
        $data = $data->order('sort desc,create_time desc')
            ->paginate($limit)
            ->toArray();
        return json_page_response($data);
    }

    /**加载添加
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        $cid = $request->param('cid');
        $obj = new Subject();
        $obj->setCid($cid);
        return $obj->getDanBiaoForm('add',$cid);
    }

    /**添加数据
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     */
    public function add(Request $request){
        $data = $request->param();
        $obj = new Subject();
        $obj->setCid($data['cid']);
        $model = $obj->getModel();
//        $data['updata_time'] = time();
        $data = $obj->addShuJu($data);
        if(Db::name($model['model_table'])->insert($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载修改
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $cid = $request->param('cid');
        $obj = new Subject();
        $obj->setCid($cid);
        return $obj->getDanBiaoForm('edit',$cid,$id);
    }

    /**查看详情
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(Request $request){
        $id = $request->param('id');
        $cid = $request->param('cid');
        $obj = new Subject();
        $obj->setCid($cid);
        return $obj->getDanBiaoForm('edit',$cid,$id,false);
    }

    /**修改
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $data = $request->param();
        $obj = new Subject();
        $obj->setCid($data['cid']);
        $model = $obj->getModel();
        $data = $obj->addShuJu($data);
        if(Db::name($model['model_table'])
            ->where('id',$data['id'])
            ->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }


    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $cid = $request->param('cid');
        $id = $request->param('id');
        $obj = new Subject();
        $obj->setCid($cid);
        $model = $obj->getModel();
        $data = Db::name($model['model_table'])->where('id',$id)->find();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Db::name($model['model_table'])->where('id',$id)->update($data)){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**删除
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $cid = $request->param('cid');
        $id = $request->param('id');
        $obj = new Subject();
        $obj->setCid($cid);
        $model = $obj->getModel();
        if(Db::name($model['model_table'])->where('id','in',$id)->update(['delete_time'=>1])){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
    public function getlianbiao(Request $request){
        $data = $request->param();
//        return json($data);
        $data = Db::name($data['table'])->where('pid',$data['pid'])->select();
        return json($data);
    }
}